<template>
  <div>
    <div class="content" v-show="userStatus.phoneState=='0'">
      <h3 class="user-title">{{'USER_PHONE'|t}}</h3>
      <div class="form">
        <zx-form-group :title="$t('MOBILE_LABEL')">
          <zx-input v-model="phoneForm.phone" type="text" :place="$t('MOBILE_PLACE')" ref="phone" :rules="rules.phone">
            <span class="input-text" slot="text" @click="getCode(phoneForm.phone,'phone',$t('GET_CODE'),$event,'phoneCodeBool')">{{'GET_CODE'|t}}</span>
          </zx-input>
        </zx-form-group>
        <zx-form-group :title="$t('CODE_LABEL')">
          <zx-input v-model="phoneForm.phone_code" :place="$t('CODE_PLACE')" ref="phone_code" :rules="rules.code">
          </zx-input>
        </zx-form-group>
        <zx-form-group>
          <input type="button" class="submit-btn mt-20 green-btn radius" :value="$t('IMMEDIATELY_BINGDING')" @click="submitForm('phoneForm')">
        </zx-form-group>
      </div>
    </div>
    <zx-user-auth :title="$t('USER_PHONE')" v-show="userStatus.phoneState=='1'" routerPath='/user/phone/change'></zx-user-auth>
  </div>
</template>
<script>
  import { mapGetters } from 'vuex';
  import { Form, Func, Auth, Rules } from '@/providers';
  import { User, UserInfo } from '@/services';
  import UserAuthText from '@/components/user-auth-text';
  export default {
    data() {
      return {
        phoneForm: {
          phone: '',
          phone_code: ''
        },
        rules: Rules
      };
    },
    methods: {
      // 获取验证码
      getCode(val, type, innerText, event, clickBool) {
        if (this[clickBool]) { return false; };
        if (!Form.validItem(this, type)) { return; };
        this[clickBool] = true;
        Form.SendMobile(val, event.target, clickBool, innerText);
      },
      async submitForm(formName, step) {
        if (!Form.validataForm(this, formName)) {
          return false;
        }
        let _parmas = this[formName];
        try {
          let res = await User.SetPhone({
            userNo: Auth.getItem('userId'),
            token: Auth.getItem('token'),
            code: _parmas.phone_code,
            phone: _parmas.phone
          });
          if (!Form.checkResToken(res)) {
            return;
          }
          this.$dialog.toast({
            position: 'top',
            msg: res.desc,
            onOk: () => {
              this.userStatus.phoneState = '1';
            }
          });
        } catch (error) {
          console.log(error);
        }
      }
    },
    computed: {
      ...mapGetters([
        'userStatus'
      ])
    },
    components: {
      'zx-user-auth': UserAuthText
    },
    created() {
    }
  };
</script>
<style lang="scss" scoped>
  @import "../../assets/sass/user.scss";
</style>